package ad.android.service.impl;

import ad.android.dao.IApplicationDao;
import ad.android.exception.DaoException;
import ad.android.exception.ServiceException;
import ad.android.model.Application;
import ad.android.service.IApplicationService;
import ad.android.service.base.BaseService;

public class ApplicationServiceImpl extends BaseService implements IApplicationService{
	private IApplicationDao appDao ;

	
	@Override
	public Application findApplicationByLicenseKey(String key)
			throws ServiceException {
		try {
			return appDao.findApplicationByLicenseKey(key);
		} catch (DaoException e) {
			log.error("findApplicationByLicenseKey",e);
			throw new ServiceException(e);
		}
	}

	
	@Override
	public String findSeckeyByLicenseKey(String licenseKey)
			throws ServiceException {
		try {
			return appDao.findSeckeyByLicenseKey(licenseKey);
		} catch (DaoException e) {
			log.error("findSeckeyByLicenseKey",e);
			throw new ServiceException(e);
		}
	}

	@Override
	public Long findApplicationIdByLicenseKey(String licenseKey)
			throws ServiceException {
		try {
			Application app = findApplicationByLicenseKey(licenseKey);
			if( app == null ){
				log.error("Application is null. LicenseKey:" + licenseKey);
				throw new ServiceException("Application is null. LicenseKey:" + licenseKey);
			}
			return app.getId();
		} catch (Exception e) {
			log.error("findApplicationIdByLicenseKey",e);
			throw new ServiceException(e);
		}
	}
	
	@Override
	public Application findApplication(Long appId) throws ServiceException {
		try {
			return appDao.findApplication(appId);
		} catch (DaoException e) {
			log.error("findApplication",e);
			throw new ServiceException(e);
		}
	}


	public IApplicationDao getAppDao() {
		return appDao;
	}

	public void setAppDao(IApplicationDao appDao) {
		this.appDao = appDao;
	}
	
}
